﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SzCourt.Logic.Common;
using KMFrame.Core;
using NBearLite;
using SzCourt.Logic.NBL;
using SzCourt.Logic.NBL.DBEntities;

namespace SzCourt.Logic.BLL
{
    public class IdeasBox : SzCourtLogic
    {        
        public PageResult GetIdeasBoxPage(PageParam condition,int UserID)
        {
            PageResult result = new PageResult();

            WhereClip where=SZCourtColumns.pIdeasBox.PublishType==2;
            if(UserID>0)
            {
                where = where || (SZCourtColumns.pIdeasBox.PublishType == 1 && SZCourtColumns.pIdeasBox.AddUserID == UserID);
            }

            result.ResultList = NBLData
                .Select(SZCourtColumns.pIdeasBox)
                .Where(where)
                .OrderBy(SZCourtColumns.pIdeasBox.AnswerDate.Desc)
                .ToList<pIdeasBox>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(SZCourtColumns.pIdeasBox, SZCourtColumns.pIdeasBox.IdeasBoxID, null);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        public bool CreateIdeasBox(int UserID, string UserName, string AnswerDept, string Questions)
        {
            bool resutl = false;

            int ri = NBLData.Insert(SZCourtColumns.pIdeasBox)
                .AddColumn(SZCourtColumns.pIdeasBox.AddUserID, UserID)
                 .AddColumn(SZCourtColumns.pIdeasBox.AddUserName, UserName)
                .AddColumn(SZCourtColumns.pIdeasBox.AddDateTime, DateTime.Now)
                .AddColumn(SZCourtColumns.pIdeasBox.AnswerDept, AnswerDept)
                .AddColumn(SZCourtColumns.pIdeasBox.Questions, Questions)
                .AddColumn(SZCourtColumns.pIdeasBox.PublishType, 0)
                .ExecuteReturnAutoIncrementID(SZCourtColumns.pIdeasBox.IdeasBoxID);
            if (ri == 1)
                resutl = true;
            return resutl;
        }

        public bool AnswerIndea(int IdeasBoxID, string AdminUserName, string Answer, short PublishType)
        {
            if (IdeasBoxID <= 0)
                return false;
            UpdateSqlSection update = NBLData.Update(SZCourtColumns.pIdeasBox)
                .AddColumn(SZCourtColumns.pIdeasBox.Answer,Answer)
                .AddColumn(SZCourtColumns.pIdeasBox.Answerer, AdminUserName)
                .AddColumn(SZCourtColumns.pIdeasBox.PublishType, PublishType)
                .AddColumn(SZCourtColumns.pIdeasBox.AnswerDate, DateTime.Now)                
                .Where(SZCourtColumns.pIdeasBox.IdeasBoxID == IdeasBoxID);

            int ri = update.Execute();
            if (ri <= 0)
                return false;
            return true;
        }

        public pIdeasBox GetIdeasBox(int IdeasBoxID)
        {
            return NBLData.Select(SZCourtColumns.pIdeasBox)
          .Where(SZCourtColumns.pIdeasBox.IdeasBoxID == IdeasBoxID)
          .ToSingleObject<pIdeasBox>();
        }

        public PageResult GetAnswerIdeasPage(PageParam condition,string AdminUserName)
        {
            PageResult result = new PageResult();
            Admin_User adminUserInfo = NBLData.Select(SZCourtColumns.Admin_User)
                .Where(SZCourtColumns.Admin_User.memb_id == AdminUserName)
                .ToSingleObject<Admin_User>();
            if (adminUserInfo == null)
                return result;           

            Admin_UserGroup adminUserGroup = NBLData.Select(SZCourtColumns.Admin_UserGroup)
                .Where(SZCourtColumns.Admin_UserGroup.ID.In(adminUserInfo.memb_group))
                .ToSingleObject<Admin_UserGroup>();

            WhereClip where = SZCourtColumns.pIdeasBox.AnswerDept == "" || SZCourtColumns.pIdeasBox.AnswerDept == "其他";
            if (adminUserGroup != null)
                where = where || SZCourtColumns.pIdeasBox.AnswerDept == adminUserGroup.GroupName;
            result.ResultList = NBLData
                .Select(SZCourtColumns.pIdeasBox)
                .Where(where)
                .OrderBy(SZCourtColumns.pIdeasBox.AnswerDate.Desc)
                .ToList<pIdeasBox>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(SZCourtColumns.pIdeasBox, SZCourtColumns.pIdeasBox.IdeasBoxID, null);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }


    }

  
}
